#include<iostream>
#include<pthread.h>
#include<unistd.h>

using namespace std;

string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(tid), "0x%lx",tid);
    return id;
}

class ThreadReturn
{
public:
    ThreadReturn(pthread_t id, string& info, int code)
            :_id(id)
            ,_info(info)
            ,_code(code)
    {}

public:
    pthread_t _id;
    string _info;
    int _code;
};

void* ThreadRountion(void* args)
{
    string name = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt--)
    {
        cout << "new thread is running!" << name << ToHex(pthread_self()) << endl;
        sleep(1);
    }

    pthread_exit((void*)10);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, ThreadRountion, (void*)"thread-1");

    int n1 = pthread_cancel(tid);
    cout << "man thread cancel: " << n1 << endl;
    cout << "main thread, " << "main thread id:" << ToHex(pthread_self()) << endl;
    
    void* ret = nullptr;
    int n2 = pthread_join(tid, &ret);
    cout << "main thread, " << "main thread id:" << n2 << endl;
    
    ThreadReturn* r = static_cast<ThreadReturn*>(ret);
    cout << "main thread get new thread info, " << r->_code << "; " << ToHex(r->_id) << r->_info << endl;
    delete r;

    return 0;
}